/*
 *所有关于PayItem类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*PayItemService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VoL.Domain.IRepositories;
using VoL.Domain.Infrastructure;
using VoL.Domain.Dtos.Patient;

namespace VoL.Domain.Services
{
    public partial class PayItemService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IBaseRepository<PayItem> _payItemRepo;
        private readonly IBaseRepository<Office> _officeRepo;
        private readonly IBaseRepository<Patient> _patientRepo;
        private readonly IBaseRepository<PayInfo> _payInfoRepo;
        private readonly IPayItemRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public PayItemService(
            IPayItemRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            IBaseRepository<PayItem> payItemRepo,
            IBaseRepository<Office> officeRepo,
            IBaseRepository<Patient> patientRepo,
            IBaseRepository<PayInfo> payInfoRepo
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _payItemRepo = payItemRepo;
            _officeRepo = officeRepo;
            _patientRepo = patientRepo;
            _payInfoRepo = payInfoRepo;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public Task<WebResponseContent> GetPayItem(int id)
        {
            var res =new  WebResponseContent();

            var payitemlist = _payItemRepo.GetAll(m => m.PayInfoId == id);

            var patientlist = _patientRepo.GetAll();

            var officelist = _officeRepo.GetAll();

            var payinfolist = _payInfoRepo.GetAll();

            var patientPayItemDTOlist = from a in payitemlist
                       join b in payinfolist on a.PayInfoId equals b.Id
                       join c in patientlist on b.patientCode equals c.PatientCode
                       join d in officelist on c.OfficeCode equals d.OfficeCode
                       select new PatientPayItemDTO()
                       {
                           Id=a.Id,
                           ItemName=a.ItemName,
                           PayInfoId=b.Id,
                           ItemType=a.ItemType,
                           Items=a.Items,
                           Price= a.Price,
                           IsReimbursement= a.IsReimbursement,
                           OfficeName=d.OfficeName,
                           RoomCode=b.RoomCode,
                           Name=c.Name,
                           PatientCode=b.patientCode,
                           StartTime=b.PayTime
                       };

            res.OK("查询表单明细成功", patientPayItemDTOlist.ToList());

            return Task.FromResult( res );
        }


   

    }

}
